import java.util.*;

public class T {



}




 class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}


 class Partition {
    public ListNode partition(ListNode pHead, int x) {



        ListNode bigTail=null;
        ListNode smallTail=null;
        ListNode cur=pHead;
        ListNode big=null;
        ListNode small=null;

        while(cur!=null){

            if (cur.val<x){
                if(small==null){
                    small=new ListNode(cur.val);
                    smallTail=small;
                }else {
                    ListNode node=new ListNode(cur.val);
                    smallTail.next=node;
                    smallTail=node;

                }
            }else {
                if(big==null){
                    big=new ListNode(cur.val);
                    bigTail=big;
                }else {
                    ListNode node=new ListNode(cur.val);
                    bigTail.next=node;
                    bigTail=node;
                }
            }
              cur=cur.next;
        }

        if(smallTail!=null){
            smallTail.next=big;
            return small;
        }else {
            return big;
        }






        // write code here
    }
}